package com.ps.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ps.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入填充...");
        // 获取当前用户ID
        Long currentId = BaseContext.getCurrentId();
        log.info("(MyMetaObjectHandler)(insertFill)当前用户ID为：{}", currentId);
        // 填充字段
        strictInsertFill(metaObject, "createUser", Long.class, currentId);
        strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        strictInsertFill(metaObject, "updateUser", Long.class, currentId);
        strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充...");
        // 获取当前用户ID
        Long currentId = BaseContext.getCurrentId();
        log.info("(MyMetaObjectHandler)(updateFill)当前用户ID为：{}", currentId);
        // 填充字段
        strictUpdateFill(metaObject, "updateUser", Long.class, currentId);
        strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
